const {
  schoolBranch,
  schoolBranchDetail
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageSize: 999,
    id: 0,
    branchList: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getSchoolBranch()
    }
    if (options.name) {
      this.setData({
        title: options.name
      })
      wx.setNavigationBarTitle({
        title: options.name,
      })
    }
  },
  async go(e) {
    const {
      id
    } = e.currentTarget.dataset
    const {
      result
    } = await schoolBranchDetail(id)
    wx.navigateTo({
      url: `/pages/article/rich-text/index?content=` + encodeURIComponent(result.content) + '&name=' + result.yxName,
    })
  },
  async getSchoolBranch() {
    const {
      id
    } = this.data
    const params = {
      pageNo: 1,
      pageSize: 999,
      schoolId: id
    }
    const {
      result
    } = await schoolBranch(params)
    const branchList = this.getBranchList(result.records)
    // console.log(branchList)
    this.setData({
      branchList
    })
  },
  getBranchList(data) {
    let map = {}
    data.forEach(item => {
      map[item.fxName] = {
        fxName: item.fxName,
        child: []
      }
    })

    data.forEach((item) => {
      if (item.fxName == map[item.fxName].fxName) {
        if (item.yxName == '' && item.yxId == '') return
        map[item.fxName].child.push(item)
      }
    })
    return map
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const {title,id}=this.data
    return {
      title: title,
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg',
      path:`/pages/school/branch/index?id=${id}&title=${title}`
    }
  }
})